/*******************************************************************************
 * Copyright (c) 2017 I3S laboratory, INRIA and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     I3S laboratory and INRIA Kairos - initial API and implementation
 *******************************************************************************/
/*
 * generated by Xtext
 */
package org.eclipse.gemoc.execution.moccml.testscenariolang.xtext;

import org.eclipse.gemoc.execution.moccml.testscenariolang.xtext.scoping.TestScenarioLangGlobalScopeProvider;
import org.eclipse.gemoc.execution.moccml.testscenariolang.xtext.scoping.TestScenarioLangScopeProvider;
import org.eclipse.xtext.conversion.IValueConverterService;
import org.eclipse.xtext.scoping.IGlobalScopeProvider;
import org.eclipse.xtext.scoping.IScopeProvider;

/**
 * Use this class to register components to be used at runtime / without the Equinox extension registry.
 */
public class TestScenarioLangRuntimeModule extends org.eclipse.gemoc.execution.moccml.testscenariolang.xtext.AbstractTestScenarioLangRuntimeModule {

	@Override
	public Class<? extends IValueConverterService> bindIValueConverterService() {
		return TestScenarioLangTerminalConverters.class;
	}
	
	/**
	 * In the standard standalone setup with the TypesGeneratorFragment enabled the interface IGlobalScopeProvider
	 * is bound to org.eclipse.xtext.common.types.xtext.TypesAwareDefaultGlobalScopeProvider. But this scope
	 * provider does not handle the importURI stuff. So we bind our own global scope provider that is both
	 * capable of handling the importURI attributes and the JVM types.
	 * (non-Javadoc)
	 * @see org.eclipse.gemoc.execution.moccml.testscenariolang.xtext.AbstractTestScenarioLangRuntimeModule#bindIGlobalScopeProvider()
	 * @see org.eclipse.xtext.common.types.xtext.TypesAwareDefaultGlobalScopeProvider
	 * @see TestScenarioLangGlobalScopeProvider
	 */
	@SuppressWarnings("restriction")       // because of links to restricted access class in the javadoc !!
	@Override
	public Class<? extends IGlobalScopeProvider> bindIGlobalScopeProvider() {
		return TestScenarioLangGlobalScopeProvider.class;
	}
	
	@Override
	public Class<? extends IScopeProvider> bindIScopeProvider() {
		return TestScenarioLangScopeProvider.class;
	}
	
	/**
	 * In both the UI and standalone setup, the ClassLoader class is bound to an instance of the class loader
	 * of the AbstractTestScenarioLangRuntimeModule instance. This instance is a DefaultClassLoader which has
	 * been used to load the org.eclipse.gemoc.execution.moccml.testscenariolang.xtext.xtext plugin/bundle.
	 * In the IU setup, the TypesGeneratorFragment binds the various typeScopeProvider to objects that are able
	 * to use the JDT to make reference to classes located in the workspace or any plugin loaded in the
	 * currently running Eclipse.
	 * In the non-UI setup, which is the one that is configured when using EMF API to manipulate Xtext
	 * resources, the typeScopeProvider is built to use the classloader specified here, and thus it relies on
	 * the classpath associated to this classloader to load classes. If a class cannot be loaded, then the 
	 * CrossLinking service of Xtext reports an error in the EMF resource we try to load. This prevents us to
	 * be able to cross reference classes from Java projects in the workspace, or from plugins that are not 
	 * dependencies of the  fiat.backend.codeexecution.xtext plugin/bundle.
	 * Solution built here has two parts:
	 * - one is to configure the Eclipse-BuddyPolicy: of the fiat.backend.codeexecution.xtext plugin to 'global'
	 *   so that its class loader is able to load class from any plugin loaded in the running Eclipse platform.
	 *   See the class org.eclipse.osgi.internal.loader.buddy.GlobalPolicy.
	 * - second part of the solution is to instanciate a special class loader which relies on the JDT to locate
	 *   classes from Java projects in the workspace. This class loader receives as its parent the class loader
	 *   of the plugin.
	 * (non-Javadoc)
	 * @see org.eclipse.gemoc.execution.moccml.testscenariolang.xtext.AbstractTestScenarioLangRuntimeModule#bindClassLoaderToInstance()
	 * @see org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader
	 * @see org.eclipse.osgi.internal.loader.buddy.GlobalPolicy
	 * @see CodeExecutionJdtClassLoader
	 */
	@SuppressWarnings("restriction")       // because of links to restricted access class in the javadoc !!
	@Override
	public java.lang.ClassLoader bindClassLoaderToInstance() {
		ClassLoader parent = getClass().getClassLoader();
		return new CodeExecutionJdtClassLoader(parent);
	}

}
